<?php
namespace Admin\Controller;
use Think\Controller;
/**
 * 后台登录处理
 * @author Jeff
 */
class LoginController extends Controller {
    
    /**
     * 加载登录页
     */
    public function index() {
        $this->display();
    }
    
    /**
     * 生成验证码
     */
    public function verify() {
        $Verify = new \Think\Verify();
		$Verify->fontSize = 30;
		$Verify->length   = 3;
		$Verify->useNoise = false;
		$Verify->entry();
    }
    /**
	 * 检测验证码
	 */
	public function check_verify($code, $id = ''){
    	$verify = new \Think\Verify();
    	return $verify->check($code, $id);
	}
    /**
     * 登录处理
     */
    public function login() {
        if (!IS_POST) {
            halt('页面不存在');
        }
        if (!$this->check_verify(I('code'))) {
            $this->error('验证码错误');
        }
        $user = M('sysuser')->where(array('username' => I('username')))->find();
        if (!$user || $user['password'] != I('password', '', 'md5')) {
            $this->error('账号或密码错误！');
        }
        session('logintime', date('Y-m-d H:i:s'), $user['logintime']);
        // 更新登录时间
        $data = array('id' => $user['id'], 'logintime' => time(), 'loginip' => get_client_ip(1));
        M('sysuser')->save($data);
        session('sysuid', $user['id']);
        session('username', $user['username']);
        session('loginip', mun_to_ip($user['loginip']));
        $this->redirect('/Admin/Index/index');
    }
	
}